#!/bin/sh
set -e -x

# Script to:
# -Install puppet and git 
# -Checkout a git repo to the /etc/puppet/modules directory
# -Apply a 'role', a role is translated to a puppet manifest found in /etc/puppet/modules/$role.pp
#
# The role to be applied can be specified as:
# 1. A parameter on the commandline
# 2. A entry in a yaml or json file

#
# Tested on:
# Ubuntu 12.04-64 'precise'
# Ubuntu 13.04 'raring'
# CentOs 6.4-64
#
# Usage:
# Download this script from:
# https://rely-dev.googlecode.com/git/files/cloud-puppet.sh
# Make executable
# chmod +x ./cloud-puppet.sh
#
# Deployment examples:
# 1. Apply the (default) development manifest on local host
# wget https://rely-dev.googlecode.com/git/files/cloud-puppet.sh && chmod +x cloud-puppet.sh && ./cloud-puppet.sh
#
# 2.
# wget https://rely-dev.googlecode.com/git/files/cloud-puppet.sh && chmod +x cloud-puppet.sh && ./cloud-puppet.sh rely_dev::developer testframework=true


# Standard role
defaultrole=rely_dev::developer

# Git repository to clone
puppet_source=https://code.google.com/p/rely-dev/

#
# Start of code
#

#
# Get latest puppet version
#

# Debian like
if [ -f /usr/bin/dpkg ]
then
  wget http://apt.puppetlabs.com/puppetlabs-release-stable.deb
  dpkg -i puppetlabs-release-stable.deb
  apt-get --yes --quiet update
  apt-get --yes -o Dpkg::Options::="--force-confold" --quiet install git puppet-common libaugeas-ruby
  rm -f puppetlabs-release-stable.deb
fi

# centos
if [ -f /bin/rpm ]
then
  rpm -Uhv --force http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-7.noarch.rpm
  curl -O http://apt.sw.be/redhat/el6/en/x86_64/extras/RPMS/git-1.7.10-1.el6.rfx.x86_64.rpm
  curl -O http://apt.sw.be/redhat/el6/en/x86_64/extras/RPMS/perl-Git-1.7.10-1.el6.rfx.x86_64.rpm
  yum -y install perl-DBI rsync
  rpm -i --force git-1.7.10-1.el6.rfx.x86_64.rpm perl-Git-1.7.10-1.el6.rfx.x86_64.rpm
  yum -y install puppet
fi

#
# Install r10k
ruby -e '`gem list -i \"^r10k$\"`.chomp=="true" or `gem install r10k`'

# Get Puppetfile
wget https://rely-dev.googlecode.com/git/Puppetfile -O /etc/puppet/Puppetfile

PUPPETFILE=/etc/puppet/Puppetfile \
PUPPETFILE_DIR=/etc/puppet/modules \
r10k puppetfile install
#
# create default puppet.conf
cat > /etc/puppet/puppet.conf << EOF
[main]
vardir = /var/lib/puppet
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = \$vardir/ssl
[agent]
pluginsync      = true
report          = true
ignoreschedules = true
environment     = production
EOF

#
# create hiradata directory
if [ ! -d "/etc/puppet/hieradata" ]; then
  mkdir /etc/puppet/hieradata
fi

#
# create hiera.yaml
cat > /etc/puppet/hiera.yaml << EOF
---
:backends:
  - json
  - yaml
:hierarchy:
  - cloud-init
  - user-data
  - global
  - default

:json:
  :datadir: /etc/puppet/hieradata
:yaml:
  :datadir: /etc/puppet/hieradata
EOF

#
# Copy meta data to hiera backend directory
#
# cloud-init json file
if [ -f /meta.js ]; then
  cp /meta.js /etc/puppet/hieradata/cloud-init.json
fi
# user-data yaml file

#
# exports facts from commandline
if [ $# -gt 1 ]; then
  export FACTER_$2
fi

#
# get role from commandline or if absent from hiera
#

if [ $# -gt 0 ]; then
  role=$1
else
  role="`hiera -c /etc/puppet/hiera.yaml role $defaultrole 2>&1`"
fi

# run puppet
puppet apply --modulepath=/etc/puppet/modules -e "include $role"



